﻿@model IPagedList<MVCApp.Models.Person>

@{
    int id = ViewBag.DistrictId;
}

@if (ViewBag.NoPersonFound != null && ViewBag.NoPersonFound == true)
{
    <div class="warningMessage">@Strings.SearchAdressNoPerson</div>
}

@if (ViewBag.SearchComplete != null && ViewBag.SearchComplete == true)
{
    <div class="infoMessage">@Strings.SearchAdressComplete</div>
}


@if (Model != null && Model.Count() != 0)
{
    <div>

        @if (Model.IsFirstPage)
        {
            if (ViewBag.NewResults == true)
            {
                <h4>@(String.Format(Strings.SearchAddressesCount, Model.TotalItemCount))</h4>
            }
            else
            {
                <h4>@(String.Format(Strings.SearchAddressesPreviousCount, Model.TotalItemCount))</h4>
            }
        }        

        <table>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.First().Selected)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.First().Name)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.First().Lastname)
                </th>
            </tr>

        @foreach (var item in Model) {
        
            string selectedClass = item.Selected?"selected" : "";
        
            <tr class="@selectedClass">
                <td class="center">
                    @Html.CheckBoxFor(
                        model => item.Selected,
                        new {
                            personId = item.Id,
                            districtId = id,
                            ajax_selector = "personSelectCheckbox",
                            id = "personSelectCheckbox_" + item.Id,
                            data_url = Url.Action("SelectPerson")
                        })
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Lastname)
                </td>
            </tr>
        }
        </table>

        @if(Model.IsLastPage)
        {
        <div class="button float-right">
            <a href="@Url.Action("Index", "DistrictAddresses",  new { id = id })" class="withImage">
                <img src='~/Images/WindowsIcons/Light/appbar.people.profile.svg'/> @Strings.DistrictAddresses
            </a>
        </div>    
            
        <div class="spacer" />
        }

        <div class="pagedList">
            @Html.PagedListPager(Model, page => Url.Action(ViewBag.NewResults == true ? "Index" : "PreviousSearch", new {id, page}),
                new PagedListRenderOptions
                {
                    Display = PagedListDisplayMode.IfNeeded,
                    MaximumPageNumbersToDisplay = 5,
                    DisplayLinkToIndividualPages = true,
                    DisplayPageCountAndCurrentLocation = true,
                    DisplayEllipsesWhenNotShowingAllPageNumbers = false,
                    DisplayLinkToFirstPage = PagedListDisplayMode.Never,
                    DisplayLinkToPreviousPage = PagedListDisplayMode.IfNeeded,
                    PageCountAndCurrentLocationFormat = Strings.PagetListPageCountAndCurrentLocationFormat,
                    DisplayLinkToLastPage = PagedListDisplayMode.Never,
                    LinkToNextPageFormat = Strings.PagedListLinkToNextPageFormat
                })
        </div>
        
    </div>
    
    @Scripts.Render("~/bundles/personSelect")
}
else
{
    if (ViewBag.NewResults == false)
    {
        <div class="messageBox">Tutaj znajdziesz wyniki poprzednich wyszukiwań, po tym jak uruchomisz aktualizację adresów.</div>
    }
}
